package com.eateateat.controller;

import com.eateateat.service.MemberService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@RestController  // @Controller+@ResponseBody
@RequestMapping("/member")
public class MemberController {
    Logger logger = Logger.getLogger(MemberController.class);

   @Autowired
   MemberService memberService;

    @GetMapping("/login")//登录hash表返回两个数据
    String slogin(HttpServletRequest request, @RequestParam Map map) throws IOException {
        logger.debug(map);//{lxmemid=哒玲玲, lxmempass=Yi123456}
        Map m=memberService.login(map);
        m.put("usertype",2);
        logger.debug(m);// {lxmemid=哒玲玲, lxmemalias=1, lxmempass=Yi123456}

        if (m!=null) {
            request.getSession().setAttribute("islogin",m);
            request.getServletContext().setAttribute((String)m.get("lxmemid"),request.getSession().getId());
            return  "success";
        } else {
            request.getSession().setAttribute("islogin", "htc_null");
            return ("failure");
        }
    }
    @GetMapping("/islogin")
    String islogin(HttpServletRequest req) throws IOException {
        Map islogin=(Map) req.getSession().getAttribute("islogin");

        logger.debug(islogin);

        if(islogin==null) {
            return "htc_null";
        }
        logger.debug(islogin);// {lxmemid=哒玲玲, lxmemalias=1, lxmempass=Yi123456}
//        return islogin.get("");
        return "hello";

    }

//    @PostMapping("/select")
//    List select(@RequestParam Map map, HttpServletRequest request)throws IOException{
//        logger.debug(map);
//        Map m= (Map) request.getSession().getAttribute("islogin");
//        logger.debug(m);
//        map.put("lxsellid",m.get("lxid"));
//        logger.debug(map);
//        List lst= memberService.getMenu(map);
//        logger.debug("insert success");
//        return lst;
//    }
    @GetMapping("/check")
    void check(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String lxmemid = request.getParameter("memid");
        logger.debug(lxmemid);
        PrintWriter out=response.getWriter();
        if(memberService.checklxmemid(lxmemid)){
            out.print("have");

        }else {
            out.print("meiyou");
        }
    }
    @PostMapping("/register")
    void register(HttpServletRequest request, HttpServletResponse response, @RequestParam Map map)throws IOException{

        logger.debug(map);
//		PrintWriter out
        if(memberService.register(map)){
            response.sendRedirect(request.getServletContext().getContextPath()+"/login.html");

        }else {
            response.sendRedirect(request.getServletContext().getContextPath()+"/register.html");
        }

    }

}
